package com.watson.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.watson.BaseActivity;
import com.watson.LoginActivity;
import com.watson.MainActivity;
import com.watson.R;
import com.watson.http.CommonHttpConsts;
import com.watson.http.HttpCallbackData;
import com.watson.http.HttpClient;
import com.watson.utils.DialogUitl;
import com.watson.utils.LogUtils;
import com.watson.utils.SPUtils;
import com.watson.utils.StringUtils;
import com.watson.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PasswordActivity extends BaseActivity {
    private AppCompatButton login_btn;
    private String password, password2;
    private LinearLayout lyLayout;
    private EditText ed_password0, ed_password, ed_password2;
    private ImageView imgEye, imgEye2;
    private TextView tvTitle;
    private boolean isEye = false;
    private boolean isEye2 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        statusbar4();

        tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("修改密码");
        findViewById(R.id.imgBack).setOnClickListener(v -> finish());
        imgEye = findViewById(R.id.imgEye);
        imgEye2 = findViewById(R.id.imgEye2);

        initView();
    }

    private void initView() {
        login_btn = findViewById(R.id.login_btn);
        ed_password0 = findViewById(R.id.ed_password0);
        ed_password = findViewById(R.id.ed_password);
        ed_password2 = findViewById(R.id.ed_password2);
        lyLayout = findViewById(R.id.lyLayout);

        imgEye.setOnClickListener(v -> {
            if (isEye) {
                isEye = false;
                ed_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                imgEye.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_eye_b));
            } else {
                isEye = true;
                ed_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                imgEye.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_eye_z));
            }
        });

        imgEye2.setOnClickListener(v -> {
            if (isEye2) {
                isEye2 = false;
                ed_password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                imgEye2.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_eye_b));
            } else {
                isEye2 = true;
                ed_password2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                imgEye2.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_eye_z));
            }
        });
        lyLayout.setOnClickListener(v -> StringUtils.hintKeyBoard(mContext, lyLayout));

        login_btn.setOnClickListener(v -> {
            password = ed_password0.getText().toString().trim();
            if (TextUtils.isEmpty(password)) {
                ToastUtil.show("请输入原密码!");
                return;
            }
            String newpassword = ed_password.getText().toString().trim();
            if (TextUtils.isEmpty(newpassword)) {
                ToastUtil.show("请输入新密码!");
                return;
            }
            password2 = ed_password2.getText().toString().trim();
            if (TextUtils.isEmpty(password2)) {
                ToastUtil.show("请再次输入新密码!");
                return;
            }
            if (!newpassword.equals(password2)) {
                ToastUtil.show("两次密码输入不一致!");
                return;
            }
            if (StringUtils.isFastClick()) {
                return;
            }
            update_Pwd(password, password2);
        });
    }

    private void update_Pwd(String account, String pwd) {
        login_btn.setEnabled(false);
        Map data = new HashMap();
        data.put("oldpwd", account);
        data.put("newpwd", pwd);
        data.put("id", SPUtils.get(mContext, "U_Id", "") + "");

        String str = new Gson().toJson(data);
        LogUtils.e("TAG", "str:" + str);
        HttpClient.getInstance().postData(CommonHttpConsts.update_Pwd, "update_Pwd")
                .headers("token", SPUtils.get(mContext, "token", "") + "")
                .upJson(str)
                .execute(new HttpCallbackData() {

                    @Override
                    public void onSuccess(int code, String msg, String info) {
                        LogUtils.e("TAG", "info:" + info);
                        ToastUtil.show(msg);
                        if (code == 1001) {
                            //SPUtils.put(mContext, "password", password2);
                            finish();
                        }
                    }

                    @Override
                    public void onError() {
                        super.onError();
                        ToastUtil.show("网络异常请稍后再试！");
                    }

                    @Override
                    public boolean showLoadingDialog() {
                        return true;
                    }

                    @Override
                    public Dialog createLoadingDialog() {
                        return DialogUitl.loginAuthDialog(mContext);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        login_btn.setEnabled(true);
                    }
                });
    }
}
